package jsyf.oa.dao.EquipmentAccount;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.dao.BaseJdbcDao;

import jsyf.oa.entity.equipmentAccount.DeviceInfoEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


@Repository
public class EquipmentDao extends BaseJdbcDao {

    public List<DeviceInfoEntity> getDeviceList(String condition, Object[] params, int requestPage, int pageSize){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("SELECT\n" +
                "t_con_main.REGION_ID AS regionId,\n" +
                "t_sys_company.NAME AS companyName,\n" +
                "t_con_main.REGION_NAME AS regionName,\n" +
                "t_con_main.REGION_MANAGER_ID as reigonManagerId,\n" +
                "t_con_main.REGION_MANAGER_NAME AS regionManagerName,\n" +
                "t_con_main.BIG_REGION_MANAGER_ID as bigRegionManagerId,\n" +
                "t_con_main.BIG_REGION_MANAGER_NAME AS bigregionManagerName,\n" +
                "t_con_main.BUYER AS customerName,\n" +
                "t_wrt_customer.TEL AS customerTel,\n" +
                "t_wrt_customer.ACCOUNT_CUSTOMER_TYPE AS accountCustomerType,\n" +
                "t_con_main.ACTUAL_CAR_BUYER AS actualCarBuyer,\n" +
                "t_con_main.ACTUAL_CAR_BUYER_TEL AS actualCarBuyerTel,\n" +
                "t_con_main.DELIVERY_DATE AS deliveryDate,\n" +
                "t_con_main.DELIVERY_ADDRESS AS deliveryAddress,\n" +
                "v_dev_device.id AS id,\n" +
                "v_dev_device.brandName AS brandName,\n" +
                "v_dev_device.cooperationType AS cooperationType,\n" +
                "v_dev_device.deviceTypeName AS deviceTypeName,\n" +
                "v_dev_device.modelName as modelName,\n" +
                "v_dev_device.sequence AS sequence,\n" +
                "v_dev_device.recentContactDate AS recentContactDate,\n" +
                "v_dev_device.latestWorkingHours AS latestWorkingHours,\n" +
                "v_dev_device.deviceSource AS deviceSource,\n" +
                "v_dev_device.warrantyState AS warrantyState,\n" +
                "v_dev_device.administratorName AS administratorName,\n" +
                "v_dev_device.administratorPhone AS administratorPhone,\n" +
                "v_dev_device.operatorName AS operatorName,\n" +
                "v_dev_device.operatorPhone AS operatorPhone,\n" +
                "v_dev_device.servicePersonnel AS servicePersonnel,\n" +
                "v_dev_device.engineNumber AS engineNumber,\n" +
                "v_dev_device.nextServiceDate AS nextServiceDate\n" +
                "FROM\n" +
                "`v_dev_device`\n" +
                "LEFT JOIN t_con_main ON t_con_main.SERIAL_NO = v_dev_device.sequence\n" +
                "LEFT JOIN t_sys_company ON t_sys_company.ID = t_con_main.SELLER_COMPANY_ID\n" +
                "LEFT JOIN t_wrt_customer ON t_con_main.BUYER_TEL=t_wrt_customer.TEL");
        stringBuffer.append(" WHERE 1 = 1 ");
        if (!StringUtils.isBlank(condition)){
            stringBuffer.append(condition);
        }
        super.appendPageSql(stringBuffer, requestPage, pageSize);

        List<JSONObject> jsonList = super.queryForJsonList(stringBuffer.toString(), params);
        List<DeviceInfoEntity> list = new ArrayList<>();

        for (int i = 0; i < jsonList.size(); i++) {
            DeviceInfoEntity model = JSON.toJavaObject(jsonList.get(i),DeviceInfoEntity.class);
            list.add(model);
        }
        return list;
    }

}
